<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Member_model');
		$this->load->model('Order_model');
		$this->load->model('Produk_model');
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->helper('bantuan_helper');
	}

	function index()
	{
		$data['page'] = 'common';
		$data['ses_id'] = $this->session->userdata('session_id');
		$data['cumb'] = anchor('home','<strong>Home</strong>').' &raquo; Login';
		$data['title'] = 'Login';
		$data['main_view'] = 'slak/login';
		$this->load->view('slak/index',$data);

	}

	function doLogout(){
		$session_id = $this->session->userdata('session_id');
		$this->Member_model->deleteTempLogin($session_id);
		$this->Order_model->deleteTempCart($session_id);
		$this->session->sess_destroy();

		redirect('home');
	}

	function doLogin()
	{
		$this->form_validation->set_rules('email', 'Email', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');

		if ($this->form_validation->run() == FALSE):
			$data['cumb'] = anchor('home','<strong>Home</strong>').' &raquo; Login';
			$data['ses_id'] = $this->session->userdata('session_id');
			$data['title'] = 'Login';
			$data['main_view'] = 'slak/login';
			$data['page'] = 'common';
			$this->load->view('slak/index',$data);
		else:
			$email = $this->input->post('email');
			$password = md5($this->input->post('password'));
			$this->db->where('email',$email);
			$this->db->where('password',$password);
			$this->db->where('status',1);
			$res = $this->db->get('tb_member')->result();

			if (count($res) > 0):
				if($res[0]->status != '0'):
					$session_id = $this->session->userdata('session_id');
					$cust_id = $res[0]->member_id;
					$email = $res[0]->email;
					$pass = $res[0]->password;
					
					$this->db->where('session_id',$session_id);
					$res_ses = $this->db->get('tb_temp_login')->result();
					
					if (count($res_ses) > 0):
						$this->session->set_flashdata('message_type','Maaf terjadi kesalahan, silahkan ulangi');
						redirect('login');
					else:
						$data = array('session_id' => $session_id,
									  'temp_mem_id' => $cust_id,
									  'temp_email' => $email,
									  'temp_password' => $pass
									  );  
						$this->Member_model->addTempLogin($data);
						$this->session->set_userdata('member_id',$res[0]->member_id);
						$this->session->set_userdata('nama',$res[0]->nama);
						$this->session->set_userdata('email',$res[0]->email);
						redirect('home');
					endif;
				else:
					$this->session->set_flashdata('message_type','Maaf account anda telah kami non-aktifkan, silahkan konfirmasi melalui kontak kami untuk mengaktifkannya kembali');
					redirect('login');
				endif;
			else:
				$this->session->set_flashdata('message_type','Maaf kombinasi email dan password anda salah, silahkan ulangi');
				redirect('login');
			endif;
		endif;
	}
}
?>